/*
 * File:   seven_main.c
 * Author: luca
 *
 * Created on 25 dicembre 2011, 15.23
 */

#include <p18f4550.h>
#include <spi.h>
#include <delays.h>

#pragma config FOSC = XT_XT
#pragma config PBADEN= OFF
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PLLDIV = 1
#pragma config MCLRE = ON
#pragma config CPUDIV = OSC1_PLL2

#pragma config USBDIV = 1
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config VREGEN = OFF
#pragma config CCP2MX = OFF
#pragma config LPT1OSC = OFF
#pragma config STVREN = OFF
#pragma config XINST = OFF
#pragma config DEBUG = OFF

#define max7219_reg_noop         0x00
#define max7219_reg_digit0       0x01
#define max7219_reg_digit1       0x02
#define max7219_reg_digit2       0x03
#define max7219_reg_digit3       0x04
#define max7219_reg_digit4       0x05
#define max7219_reg_digit5       0x06
#define max7219_reg_digit6       0x07
#define max7219_reg_digit7       0x08
#define max7219_reg_decodeMode   0x09
#define max7219_reg_intensity    0x0a
#define max7219_reg_scanLimit    0x0b
#define max7219_reg_shutdown     0x0c
#define max7219_reg_displayTest  0x0f

#define LOAD PORTAbits.RA5


void init(void);
void delay1s(void); //verificato!! 1 secondo!
void delay250ms(void);
void delay125ms(void);

void main(void) {

    int i=0,j=0, k=0, z=0;

    TRISAbits.RA5=0; //ouput
    ADCON0=0;
    ADCON1=1;
    ADCON2=0;
    
    LOAD=0;

    OpenSPI(SPI_FOSC_4, MODE_00, SMPEND);

    LOAD=1;
    LOAD=0;

    init();

     

    for(i=1;i<5;i++){

        for(j=9;j>=0;j--){
            LOAD=0;
            WriteSPI(i);
            WriteSPI(j);
            LOAD=1;
            //250ms
            Delay10KTCYx(5);
        }
        LOAD=0;
        WriteSPI(i);
        WriteSPI(0x00);
        LOAD=1;
        //250ms
         Delay10KTCYx(5);
    }
    
    j=0;
    k=0;
    z=0;
    while(1){

       /* for (j=0;j<10;j++){

             LOAD=0;
             WriteSPI(max7219_reg_digit1);
             WriteSPI(j);
             LOAD=1;
             LOAD=0;*/

             //decine
             LOAD=0;
             WriteSPI(0x02);
             WriteSPI(j);
             LOAD=1;
             LOAD=0;
             //centinaia
             LOAD=0;
             WriteSPI(0x03);
             WriteSPI(k);
             LOAD=1;
             LOAD=0;

             //migliaia
             LOAD=0;
             WriteSPI(0x04);
             WriteSPI(z);
             LOAD=1;
             LOAD=0;



             for(i=0;i<10;i++){

                LOAD=0;
                WriteSPI(max7219_reg_digit0);
                WriteSPI(i);
                LOAD=1;
                LOAD=0;

                delay125ms();
              }

             
             j++;
             if(j>=10) {
                 j=0;
                 k++;}

             if(k>=10) {
                 k=0;
                 z++;
             }
             if(z>=10) z=0;

        //}//for esterno

        
    }//while




}//fine main


void delay1s(void){
    
    Delay10KTCYx(100);
    
    
    
}

void delay250ms(void){

    Delay10KTCYx(25);


}
void delay125ms(void){

    Delay10KTCYx(6);


}


void init(void)
{
    int k = 0;
LOAD = 0;       // SELECT MAX
WriteSPI(0x09);      // BCD mode for digit decoding
WriteSPI(0xFF);
LOAD = 1;       // DESELECT MAX
LOAD = 0;


LOAD = 0;       // SELECT MAX
WriteSPI(0x0A);
WriteSPI(0x0F);      // Segment luminosity intensity
LOAD = 1;       // DESELECT MAX
LOAD = 0;


LOAD = 0;       // SELECT MAX
WriteSPI(0x0B);
WriteSPI(0x07);      // Display refresh
LOAD = 1;       // DESELECT MAX
LOAD = 0;


LOAD = 0;       // SELECT MAX
WriteSPI(0x0C);
WriteSPI(0x01);      // Turn on the display
LOAD = 1;       // DESELECT MAX
LOAD = 0;




LOAD = 0;       // SELECT MAX
WriteSPI(0x0F);
WriteSPI(0x0);      // No test
LOAD = 1;       // DESELECT MAX
LOAD = 0;


for(k=1;k<5;k++){
        LOAD=0;
        WriteSPI(k);
        WriteSPI(0x00);
        LOAD=1;
     }
}